# -*- coding = utf-8 -*-
# @Time : 2021/3/13 14:43
# @Author : yzh
# @File : test.py
# @Software : PyCharm

import requests
import re
'''
查答案
args topic: 传入题目关键词，查找答案
'''


def checkAnswer(topic):
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3775.400 QQBrowser/10.6.4208.400"
    }
    req = requests.get('http://172.81.205.2:8080/', headers=headers)
    cookie = req.headers['set-cookie']
    csrfToken = re.findall(r'csrfmiddlewaretoken\' value=\'(.*?)\'', req.text)[0]

    headers = {
        'Accept': '*/*',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3775.400 QQBrowser/10.6.4208.400',
        'Referer': 'http://172.81.205.2:8080/',
        'Origin': 'http://172.81.205.2:8080',
        'Connection': 'keep-alive',
        'Host': '172.81.205.2:8080',
        "Cookie": cookie
    }

    data = {
        "csrfmiddlewaretoken": csrfToken,
        "question": topic
    }

    res = requests.post('http://172.81.205.2:8080/index', data=data,headers=headers)
    comment = re.compile(r'<ul>(.*?)</ul>',re.DOTALL)
    ans = comment.findall(res.text)
    return ans


if __name__ == '__main__':
    while True:
        kw = input('直接输入任意词组作为关键词，2-4字即可，输入q可退出：')
        if kw == 'q':
            break

        checkAnswer(kw)
        print('-' * 50)